package roby.privateEncryption;

import java.util.ArrayList;

public class PrivateDecryptor {
	
	private byte[] key;
	
	public PrivateDecryptor(){
		
		key = KeyGenerator.getKey();
		
	}
	public PrivateDecryptor(byte[] key){
		this.key = key;
	}	

	
	public String decrypt(String cipherText){
		System.out.println("Cipher Text: " + cipherText);
		String plainText = null;
		int size = cipherText.length();
		ArrayList<String> stringList = new ArrayList<String>();
		if(size > 1024){
			for(int i = 0; i < size; i+= 1024){
				stringList.add(cipherText.substring(i, i+1024));
			}
		}
		else{
			stringList.add(cipherText);
		}
		byte[] decryptedMessage = new byte[size];
		int index = 0;
		for(int i = 0; i < stringList.size(); i++){
			byte[] string = stringList.get(i).getBytes();
			for(int j = 0; j < size ; j++){
				
				decryptedMessage[index] = (byte) (key[index%1024] ^ string[j]);
				index++;
				
			}
		}
		plainText = new String(decryptedMessage);
		System.out.println("Decrypted Message: " + plainText);
		return plainText;
	}

}
